<?php
//error_reporting(0);
	include_once 'isLogin.php';
    
	$table_name = "companies_categories";
	$key_index = array('title','content','upid','state');
	
	$colors = array(
				'state'=>array('0'=>'#ff0000','1'=>'#16d316'),
			);
	$names = array(
				'state'=>array('0'=>'查看下级','1'=>'最后一级'),
			);
	$level = 1;
	//添加
	if($_GET['action']=='add'){
		
		//插入数据
		if(is_array($_POST['values'])){
			foreach($_POST['values'] as $k=>$v){
				$arr[$key_index[$k]] = $v;
			}
		}
		$commonc->insert($table_name,$arr);
		$id = $commonc->insert_id();
		
		//排序
		$arr = array('sort'=>$id);
		$commonc->update($table_name, $arr, "id='".$id."'");
		
		//查询记录
		$data = $commonc->getInfo($table_name, $id);
		
		if($id!=''){
			$return='<tr line_id="'.$id.'">
			<td> <input type="checkbox" name="checkbox" value="'.$id.'" /></td>
			<td value_id="'.$id.'">'.$data['title'].'</td>
			<td value_id="'.$id.'">'.$data['content'].'</td>
			<td>&nbsp;</td>
			<td class="xiugai">
				<a href="javascript:;" onclick="sort($(this),\''.$id.'\',\'up\',\''.$_basename.'?action=sort\')"><img src="images/sort-up.png"/></a>
				<a href="javascript:;" onclick="sort($(this),\''.$id.'\',\'down\','.$_basename.'?action=sort\')"><img src="images/sort-down.png"/></a>
			</td>
			<td class="xiugai"><a href="javascript:;" style="color: '.$colors['state'][$data['state']].'" onclick="state(\''.$id.'\',\'state\',\''.$_basename.'\',$(this))">'.$names['state'][$data['state']].'</a></td>
			<td class="xiugai">
			<a href="javascript:;" onclick="edit(\''.$id.'\',$(this),\''.$_basename.'?action=edit\')">修改</a>';
			if($data['upid']==false) $return.='<a href="'.$_basename.'?id='.$id.'">子类</a>';
			$return.='</td></tr>';
			$arr=array('error'=>'0','content'=>$return);
		}else{
			$arr=array('error'=>'1','content'=>'添加失败');
		}
		echo json_encode($arr);
		exit();
	}
	
	//删除
	if($_GET['action']=='del'){
		
		echo $commonc->del($table_name,$_POST['id']);
		exit();
	}
	
	//修改
	if($_GET['action']=='edit'){
		$arr = array($key_index[$_POST['index']]=>$_POST['value']);
		echo $commonc->update($table_name,$arr,"id='".$_POST['value_id']."'");
		exit();
	}
	
	//修改状态
	if($_GET['action']=='state'){
		echo $commonc->changeState($table_name,$_POST['id'],$_POST['key'],$colors[$_POST['key']],$names[$_POST['key']]);
		exit();
	}
	
	//排序
	if($_GET['action']=='sort'){
		if($_POST['type']=='down') $type = "prev";
		if($_POST['type']=='up') $type = "next";
		$sort = $commonc->sortChange($table_name, $_POST['id'], $type);
		echo $sort['id'];
		exit();
	}
	
    //所有分类层级
	$all_categories_level = $commonc->getList($table_name." order by level ASC",'','level');
	foreach($all_categories_level ["result"] as $values)
	{
		foreach($values as $value)
		{
			$levels_unique[] = $value;
		}
	}
	$levels_unique = array_unique($levels_unique);
	$i = 1;
	foreach($levels_unique as $k=>$v)
	{
		unset($levels_unique[$k]);
		$arr['level'] = $v;
		$arr['id'] = $i;
		$levels[] = $arr;
		$i++;
	}
	
	//搜索
	if($_POST['search']!=''&&$_POST['search']!=null)
	{
		$search_key = $_POST['name'];
		$level = $_POST['level'];
		$smarty->assign("search" , $_POST);
		
		if($search_key!=''&&$search_key!=null)
		{
			$where =' where'; 
			$where .= " title like '%".$search_key."%' ";
		}
		if($level!=''&&$level!=null)
		{
			if($search_key!=''&&$search_key!=null)
			{
				$where .= " and level = '".$level."'";
			}
			else
			{
				$where =' where'; 
				$where .= " level = '".$level."'";
			}
		}
	}
	else
	{
		//得到所有内容
		if($_GET['id']!=''){
		
			$level = $_GET['level'];
			//$level += 1;  
			$upid = $_GET['id'];
			$where = " where upid='".$_GET['id']."'  AND level= '".$level."' ";
		}else{
			$upid = '0';
			$where = " where upid='0' AND level= '".$level."' ";
		}
	}
	
	$smarty->assign("upid",$upid);
	$sql = $table_name;
	$sql.= $where;
	$sql.= " order by sort ASC";
	$row=$commonc->getList($sql,$sql);
	$rowt=$commonc->getList("companies c left join companies_categories g on c.upid=g.sort where c.upid='".$_GET['id']."' order by g.sort DESC", $sqlnum='', $key="c.title as name,c.id",$debug='0');
	foreach($rowt as $key=>$value){
	}
	if(is_array($row['result'])){
		//得到是否有下级分类
		
		$row['result'] = $commonc->is_nextcategories($table_name,$colors['state'],$names['state'],$row['result'],'state');
	}
	if ($row['result'][0]['upid']!=''&&$row['result'][0]['upid']!=null) {
	    $smarty->assign("upid",$row['result'][0]['upid']);
	}
	$smarty->assign("row",$row['result']);
	$smarty->assign("value",$value);
	$smarty->assign("levels",$levels);
	$smarty->assign("level",$level);
	$smarty->assign('page',$row['page']);
	$smarty->display($_filename.".html");
?>